package com.cap.dreamcircle.Utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * Created by liukanshan on 2016/12/8.
 */

public class TimeUtils {

    /**
     * 获取Unix时间戳
     *
     * @return
     */
    public static String GetUnixTimeStamp() {
        return System.currentTimeMillis() / 1000 + "";
    }

    public static String formatTime(String postDate) {
        String result = "";
        if (TextUtils.isEmpty(postDate)) return result;
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(postDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        calendar.setTime(date);
//        Calendar calendar1 = Calendar.getInstance();
//        calendar1.setTimeInMillis(System.currentTimeMillis());
//
//        long sumMinute = (System.currentTimeMillis() - date.getTime()) / 1000;
//        if (sumMinute < 0) {
//            return "";
//        } else {
//            if (sumMinute >= 365 * 24 * 60*60) {
//                return (int) sumMinute / (365 * 24 * 60*60)+ "年前";
//            }
//            else if(sumMinute >= 30 * 24 * 60*60){
//                return (int) sumMinute / (30 * 24 * 60*60) + "月前";
//            }
//            else if(sumMinute >= 24 * 60*60){
//                return (int) sumMinute / (24 * 60*60)+ "天前";
//            }
//            else if(sumMinute >= 1 * 60*60){
//                return (int) sumMinute / (1 * 60*60)+ "小时前";
//            }
//            else if(sumMinute >= 1 * 60){
//                return (int) sumMinute / (1 * 60)+ "分钟前";
//            }
//            else{
//                return (int) sumMinute + "秒前";
//            }
//        }
        Calendar otherCalendar = Calendar.getInstance();
        otherCalendar.setTime(date);
        if ((calendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == otherCalendar.get(Calendar.MONTH)) && (calendar.get(Calendar.DAY_OF_MONTH) == otherCalendar.get(Calendar.DAY_OF_MONTH))) {
            SimpleDateFormat sdf_local = new SimpleDateFormat("HH:mm");
            return sdf_local.format(date);
        }
        if ((calendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR))) {
            SimpleDateFormat sdf_local = new SimpleDateFormat("MM-dd");
            return sdf_local.format(date);
        } else {
            SimpleDateFormat sdf_local = new SimpleDateFormat("yyyy-MM-dd");
            return sdf_local.format(date);
        }
    }
}
